# -*- coding: utf-8 -*-
import unittest
import os, sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
from api.base_api.base_api import BaseApi
from api.base_api.login_api import LoginApi



class UserRegisterTest(BaseApi):
    ''' 登录 '''

    def setUp(self):
        self.mobile = '13900000000'
        self.error_mobile = '13100000000'
        self.password = '123abc'
        self.error_password = '123456'
        self.error_captcha_code = 'aaaa'

    def test_user_login_success(self):
        ''' 登录成功 '''

        self.response = LoginApi().login(self.mobile, self.password)
        status_code = self.response.status_code
        self.assertEqual(status_code, 200)

        token = self.response.json()['token']
        self.assertIsNotNone(token)
        self.assertEqual(len(token),36)

    def test_user_password_fail(self):
        ''' 密码错误 '''

        self.response = LoginApi().login(self.mobile, self.error_password)
        status_code = self.response.status_code
        self.assertNotEqual(status_code, 200)

        message = self.response.json()['message']
        self.assertEqual(message, '密码应为6-16位数字字母组合')

    def test_user_mobile_fail(self):
        ''' 手机号未注册 '''
        self.response = LoginApi().login(self.error_mobile, self.password)
        status_code = self.response.status_code
        self.assertNotEqual(status_code, 200)

        message = self.response.json()['message']
        self.assertEqual(message, '手机号或密码错误')

    def test_user_captcha_code_fail(self):
        ''' 验证码错误 '''
        self.response = LoginApi().login(self.error_mobile, self.password, self.error_captcha_code)
        status_code = self.response.status_code
        self.assertNotEqual(status_code, 200)

        message = self.response.json()['message']
        self.assertEqual(message, '验证码错误')


if __name__ == '__main__':
    #用例默认执行顺序是0-9,a-z,所以先执行test_user_again_register
    #构造测试集

    unittest.main()
